package myApp

import (
	"encoding/json"
	serverCommon "fiberCommonUtils/common"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"os"
)

type AppSettingJson struct {
	Wx struct {
		WxpayNotifyUrl      string `json:"WxpayNotifyUrl"`
		WxMpAppId           string `json:"WxMpAppId"`
		WxMpAppSecret       string `json:"WxMpAppSecret"`
		MchID               string `json:"MchID"`
		WxPayV3ApiSecret    string `json:"WxPayV3ApiSecret"`
		WxPayApiSecret      string `json:"WxPayApiSecret"`
		WxPayCASerialNumber string `json:"WxPayCASerialNumber"`
		SpAppid             string `json:"SpAppid"`
		SpMchid             string `json:"SpMchid"`
		WxPayPrivateKeyPath string `json:"WxPayPrivateKeyPath"`
	} `json:"wx"`
	Oss struct {
		EndPoint        string `json:"EndPoint"`
		BucketName      string `json:"BucketName"`
		AccessKeyId     string `json:"AccessKeyId"`
		AccessKeySecret string `json:"AccessKeySecret"`
	} `json:"oss"`
	Mongo struct {
		TestURI    string `json:"testURI"`
		ReleaseURI string `json:"ReleaseURI"`
		DBName     string `json:"dbName"`
	} `json:"mongo"`
	Site struct {
		SERVICEPHONE               string `json:"SERVICEPHONE"`
		SERVICEWX                  string `json:"SERVICEWX"`
		TITLEOFTICKET              string `json:"TITLEOFTICKET"`
		OutsideVisit               string `json:"Outside-visit"`
		CRON                       bool   `json:"CRON"`
		OrderMustFinishEveryday    bool   `json:"OrderMustFinishEveryday"`
		TollCanAFK                 bool   `json:"TollCanAFK"`
		ClientOperateCanPauseOrder bool   `json:"ClientOperateCanPauseOrder"`
		DisabledClientCallPayCash  bool   `json:"DisabledClientCallPayCash"`
	} `json:"site"`
	Jwt struct {
		SignKey string `json:"SignKey"`
	} `json:"jwt"`
	StudyMachineServer struct {
		Url string `json:"url"`
	} `json:"studyMachineServer"`
}

var settingPath = ""
var AppSetting *AppSettingJson

func InitAppSetting() {
	path := os.Getenv("SettingFilePath") //在环境变量里指定setting位置
	if path != "" {
		settingPath = path
	}

	fileBytes, err := os.ReadFile(settingPath)
	if err != nil {
		panic("配置json文件读取错误" + err.Error())
		return
	}
	AppSetting = new(AppSettingJson)
	err = json.Unmarshal(fileBytes, AppSetting)
	if err != nil {
		panic("配置json文件解码错误" + err.Error())
		return
	}
	fmt.Println("配置读取结束")
}

// AppSettingResponse 用以给外部各种程序获取的一些服务器基本配置
type AppSettingResponse struct {
	Oss struct {
		EndPoint   string `json:"endPoint"`
		BucketName string `json:"bucketName"`
		Prefix     string `json:"prefix"`
	} `json:"oss"`
	Site struct {
		SERVICEPHONE  string `json:"SERVICEPHONE"`
		SERVICEWX     string `json:"SERVICEWX"`
		TITLEOFTICKET string `json:"TITLEOFTICKET"`
	} `json:"site"`
}

func GetServerInformation(c *fiber.Ctx) error {
	result := AppSettingResponse{}
	result.Oss.EndPoint = AppSetting.Oss.EndPoint
	result.Oss.BucketName = AppSetting.Oss.BucketName
	result.Oss.Prefix = AppSetting.Oss.BucketName + AppSetting.Oss.EndPoint
	result.Site.SERVICEPHONE = AppSetting.Site.SERVICEPHONE
	return c.JSON(serverCommon.MakeSuccessJson(&result))
}
